using System;
using System.Configuration;
using System.Diagnostics;
using System.Data.SqlClient;

namespace DotConferenceSql
{
    /// <summary>
    /// An exception related to DatabaseConnection class
    /// </summary>
    public class DatabaseConnectionException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseConnectionException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public DatabaseConnectionException(string message)
            : base(message)
        {
        }
    }

    /// <summary>
    /// DatabaseConnection manage a single connection to
    /// a SQL Server database
    /// </summary>
    public class DatabaseConnection : IDisposable
    {
        private SqlConnection m_connection = null;

        /// <summary>
        /// Gets the internal SQLConnection.
        /// </summary>
        /// <value>The connection.</value>
        public SqlConnection Connection
        {
            get
            {
                return m_connection;
            }
        }

        #region IDisposable Membres

        /// <summary>
        /// Closes database connection on resource release
        /// </summary>
        public void Dispose()
        {
            Close();
        }

        #endregion

        /// <summary>
        /// Opens connection
        /// </summary>
        public void Open()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MainSqlConnection"].ConnectionString;
            m_connection = new SqlConnection(connectionString);

            try
            {
                m_connection.Open();
                Debug.WriteLine("DotConferenceSql.DatabaseConnection.open(): SQL Connection open.");
            }
            catch (SqlException ex)
            {
                Debug.WriteLine("DotConferenceSql.DatabaseConnection.open(): " + ex.Message);
                throw new DatabaseConnectionException(ex.Message);
            }
            catch (Exception generalEx)
            {
                Debug.WriteLine("DotConferenceSql.DatabaseConnection.open(): " + generalEx.Message);
                throw new DatabaseConnectionException(generalEx.Message);
            }
        }

        /// <summary>
        /// Closes the connection
        /// </summary>
        public void Close()
        {
            try
            {
                Connection.Close();
                Debug.WriteLine("DotConferenceSql.DatabaseConnection.close(): Closing SQL connection...");
            }
            catch(Exception ex)
            {
                throw new DatabaseConnectionException(ex.Message);
            }
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="DotConferenceSql.DatabaseConnection"/> to <see cref="System.Data.SqlClient.SqlConnection"/>.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>The result of the conversion.</returns>
        static public implicit operator SqlConnection(DatabaseConnection connection)
        {
            return connection.Connection;
        }
    }
}
